--#include "data\functions\Activity\OpenServer\OpenServerCommon.lua" once
--开服累计充值

-- 活动结束
function OpenServerTotalRechargeEnd()
    if not IsInOpenServerToTalRechargeEndDay() then
        return
    end
    local config = OpenServerCfg.Recharge
    if not config then return end

    local rankPtr = Ranking.getRanking(RankList[RANK_OS_TOTAL_RECHARGE].strName)
    local rankCount = Ranking.getRankItemCount(rankPtr)
    for idx = 1, rankCount do
        local item = Ranking.getItemFromIndex(rankPtr, idx-1)
        if item then
            local actorid = tonumber(Ranking.getId(item))
            local data = Ranking.getSub(item, 1)
            local dataTable
            if not data or data == "" then
                dataTable = {}
            else
                dataTable = BaseFuc_unserialize(data)
            end
          
            if dataTable.myGoldData then
                local rewardCfg = config.Rewards
                for idx = 1, #config.Gold do
                    if dataTable.myGoldData[idx] and dataTable.myGoldData[idx] == 1 then
                        local rewards = rewardCfg[idx]
                        local title = Lang.ScriptTips.OpenServerTotalRecharge001
                        local content = Lang.ScriptTips.OpenServerTotalRecharge002
                        Awards.GiveToMailById(actorid, rewards, title, content, LogIds.OpenServerTotalRechargeLogId, Lang.LogTips.OpenServerTotalRecharge, dataTable.vocation, dataTable.sex)
                    end
                end
            end
        end
    end
end

-- 打包数据
function OpenServerTotalRechargeSendData(sysarg)
    if not IsInOpenServerToTalRechargeDay() then
        return
    end
    
    local config = OpenServerCfg.Recharge
    if not config then return end

    local actorid = Actor.getActorId(sysarg)
    local data = gRankMgr_GetValueById(actorid, RANK_OS_TOTAL_RECHARGE, 1)

    local myData = BaseFuc_unserialize(data)
    

    if not myData then
        myData = {}
    end

    if not myData.myGold then
        myData.myGold = 0
    end
    if not myData.myGoldData then
        myData.myGoldData = {}
    end


   local pack = DataPack.allocPacket(sysarg, LogicCmd.miscSys.sysId, LogicCmd.miscSys.sub.sGetOpenServerTotalRechargeInfo)
    if not pack then return end
    
    DataPack.writeUInt(pack, myData.myGold or 0)
    local len = #config.Gold
    DataPack.writeByte(pack, len)
    for idx = 1, len do
        local state = myData.myGoldData[idx] or 0
        DataPack.writeByte(pack, state)
    end
    DataPack.flush(pack)
    
    --DataPack.writeByte(netPack, 1)        -- 活动特有参数
end

-- 操作，返回操作结果，0为成功
function OpenServerTotalRechargeHandler(sysarg, clientPkt)
    if not IsInOpenServerToTalRechargeDay() then
        return
    end
    
    local config = OpenServerCfg.Recharge

    local Index = DataPack.readByte(clientPkt)
    
    local ssVar = System.getStaticVar()
    
    if Index > #config.Gold then
        Actor.sendTipmsg(sysarg, Lang.ScriptTips.OpenServerTotalRecharge004, ttFlyTip)
        return
    end

    local actorid = Actor.getActorId(sysarg)
    local data = gRankMgr_GetValueById(actorid, RANK_OS_TOTAL_RECHARGE, 1)
    local myData = BaseFuc_unserialize(data)

    if not myData then
        Actor.sendTipmsg(sysarg, Lang.ScriptTips.OpenServerTotalRecharge005, ttFlyTip)
        return
    end
    if not myData.myGoldData then
        Actor.sendTipmsg(sysarg, Lang.ScriptTips.OpenServerTotalRecharge005, ttFlyTip)
        return
    end
    if not myData.myGoldData[Index] or myData.myGoldData[Index] ~= 1 then
        Actor.sendTipmsg(sysarg, Lang.ScriptTips.OpenServerTotalRecharge005, ttFlyTip)
        return
    end

    local rewardCfg = config.Rewards

    local rewards = rewardCfg[Index]

    myData.myGoldData[Index] = 2
    
    if CheckBagGridForAwardsEx(sysarg, rewards) == 0 then
        GiveCommonAwardEx(sysarg, rewards, LogIds.OpenServerTotalRechargeLogId, Lang.LogTips.OpenServerTotalRecharge)
    else
        local actorId = Actor.getActorId(sysarg)
        local title = Lang.ScriptTips.OpenServerTotalRecharge001
        local content = Lang.ScriptTips.OpenServerTotalRecharge002
        local nActorJob = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
        local nActorSex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
        Awards.GiveToMailById(actorId, rewards, title, content, LogIds.OpenServerTotalRechargeLogId, Lang.LogTips.OpenServerTotalRecharge, nActorJob, nActorSex)
    end

    local sActorName = Actor.getName(sysarg)
    local msg = string.format(Lang.ScriptTips.OpenServerTotalRecharge003, sActorName, Index, enLinkOSTotalRechargeWnd)
    System.broadcastTipmsg(msg,ttScreenCenter + ttChatWindow)

    if not myData.sex then
        myData.sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
        myData.vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
    end
    

    local newData = BaseFuc_serialize(myData)
    gRankMgr_SetRankEx(actorid, RANK_OS_TOTAL_RECHARGE, 1, newData)
        
    local pack = DataPack.allocPacket(sysarg, LogicCmd.miscSys.sysId, LogicCmd.miscSys.sub.sGetOpenServerTotalRechargeAward)
    if not pack then return end
    
    DataPack.writeByte(pack, Index)
    DataPack.writeByte(pack, 2)
    DataPack.flush(pack)
    
end


--累计充值元宝
function OpenServerTotalRechargeWithDrawYuanbao(sysarg, Gold)
    if not IsInOpenServerToTalRechargeDay() then
        return
    end
    local config = OpenServerCfg.Recharge
    if not config then
        return
    end 

    local actorid = Actor.getActorId(sysarg)
    local data = gRankMgr_GetValueById(actorid, RANK_OS_TOTAL_RECHARGE, 1)

    local myData = BaseFuc_unserialize(data)

    if not myData then
        myData = {}
    end
    if not myData.myGold then
        myData.myGold = 0
    end
    myData.myGold = myData.myGold + Gold
    if not myData.myGoldData then
        myData.myGoldData = {}
    end
    for idx = 1, #config.Gold do
        if myData.myGoldData[idx] and myData.myGoldData[idx] >= 1 then
        else
            if config.Gold[idx] <= myData.myGold then
                myData.myGoldData[idx] = 1
            end
        end
    end

    if not myData.sex then
        myData.sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
        myData.vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
    end

    local newData = BaseFuc_serialize(myData)
    gRankMgr_SetRankEx(actorid, RANK_OS_TOTAL_RECHARGE, 1, newData)
    OpenServerTotalRechargeSendData(sysarg)
end

function IsInOpenServerToTalRechargeDay()
    local openday = System.getDaysSinceOpenServer()
    local startDay = OpenServerCfg.Recharge.startDay or 1
    local endDay = OpenServerCfg.Recharge.endDay or OpenServerCfg.OpenServerDay
    if openday >= startDay and openday <= endDay then
        return true
    end
    return false
end

function IsInOpenServerToTalRechargeEndDay()
    local openday = System.getDaysSinceOpenServer()
    local startDay = OpenServerCfg.Recharge.startDay or 1
    local endDay = OpenServerCfg.Recharge.endDay or OpenServerCfg.OpenServerDay
    if openday > endDay and openday <= endDay + 1 then
        return true
    end
    return false
end


function OpenServerTOtalRechargeSpecialLogin(sysarg)
    if not IsInOpenServerToTalRechargeDay() then
        return
    end
    local sVar = Actor.getStaticVar(sysarg)
    if sVar.osTotalRechargeSpecial then
        return
    end
    sVar.osTotalRechargeSpecial = 1
    local Gold = Actor.getIntProperty(sysarg, PROP_ACTOR_DRAW_YB_COUNT)
    if Gold > 0 then
        OpenServerTotalRechargeWithDrawYuanbao(sysarg, Gold)
    end
end


------------------------------开服每日充值------------------------------------
-- 活动结束
function OpenServerDailyTotalRechargeEnd()
    if not IsInOpenServerDailyEndDay() then
        return
    end
    local day = System.getDaysSinceOpenServer()
    local config = OpenServerCfg.DailyRecharge[day - 1]
    if not config then return end

    local rankPtr = Ranking.getRanking(RankList[RANK_OS_DAILYTOTAL_RECHARGE].strName)
    local rankCount = Ranking.getRankItemCount(rankPtr)
    for idx = 1, rankCount do
        local item = Ranking.getItemFromIndex(rankPtr, idx-1)
        if item then
            local actorid = tonumber(Ranking.getId(item))
            local data = Ranking.getSub(item, 1)
            local dataTable
            if not data or data == "" then
                dataTable = {}
            else
                dataTable = BaseFuc_unserialize(data)
            end
          
            if dataTable.myGoldData then
                local rewardCfg = config.Rewards
                for idx = 1, #config.Gold do
                    if dataTable.myGoldData[idx] and dataTable.myGoldData[idx] == 1 then
                        local rewards = rewardCfg[idx]
                        local title = Lang.ScriptTips.OpenServerDailyTotalRecharge001
                        local content = Lang.ScriptTips.OpenServerDailyTotalRecharge002
                        Awards.GiveToMailById(actorid, rewards, title, content, LogIds.OSDailyTotalRechargeLogId, Lang.LogTips.OpenServerDailyTotalRecharge, dataTable.vocation, dataTable.sex)
                    end
                end
            end
        end
    end

    gRankMgr_Clear(RANK_OS_DAILYTOTAL_RECHARGE)
end

-- 打包数据
function OpenServerDailyTotalRechargeSendData(sysarg)
    if not IsInOpenServerDay() then
        return
    end
    
    local day = System.getDaysSinceOpenServer()
    local config = OpenServerCfg.DailyRecharge[day]
    if not config then return end

    local actorid = Actor.getActorId(sysarg)
    --print("actorid ="..actorid)
    local data = gRankMgr_GetValueById(actorid, RANK_OS_DAILYTOTAL_RECHARGE, 1)
    --[[
    if data then
        print("data ="..data)
    end
    ]]

    local myData = BaseFuc_unserialize(data)
    

    if not myData then
        myData = {}
    end

    if not myData.myGold then
        myData.myGold = 0
    end
    if not myData.myGoldData then
        myData.myGoldData = {}
    end


   local pack = DataPack.allocPacket(sysarg, LogicCmd.miscSys.sysId, LogicCmd.miscSys.sub.sGetOpenServerDailyTotalRechargeInfo)
    if not pack then return end
    
    DataPack.writeUInt(pack, myData.myGold or 0)
    --print("myData.myGold ="..(myData.myGold or 0))
    local len = #config.Gold
    DataPack.writeByte(pack, len)
    for idx = 1, len do
        local state = myData.myGoldData[idx] or 0
        --print("state = "..state)
        DataPack.writeByte(pack, state)
    end
    DataPack.flush(pack)
    
    --DataPack.writeByte(netPack, 1)        -- 活动特有参数
end

-- 操作，返回操作结果，0为成功
function OpenServerDailyTotalRechargeHandler(sysarg, clientPkt)
    if not IsInOpenServerDay() then
        return
    end
    
    local day = System.getDaysSinceOpenServer()
    local config = OpenServerCfg.DailyRecharge[day]

    local Index = DataPack.readByte(clientPkt)
    
    local ssVar = System.getStaticVar()
    
    if Index > #config.Gold then
        Actor.sendTipmsg(sysarg, Lang.ScriptTips.OpenServerDailyTotalRecharge004, ttFlyTip)
        return
    end

    local actorid = Actor.getActorId(sysarg)
    local data = gRankMgr_GetValueById(actorid, RANK_OS_DAILYTOTAL_RECHARGE, 1)
    local myData = BaseFuc_unserialize(data)

    if not myData then
        Actor.sendTipmsg(sysarg, Lang.ScriptTips.OpenServerDailyTotalRecharge005, ttFlyTip)
        return
    end
    if not myData.myGoldData then
        Actor.sendTipmsg(sysarg, Lang.ScriptTips.OpenServerDailyTotalRecharge005, ttFlyTip)
        return
    end
    if not myData.myGoldData[Index] or myData.myGoldData[Index] ~= 1 then
        Actor.sendTipmsg(sysarg, Lang.ScriptTips.OpenServerDailyTotalRecharge005, ttFlyTip)
        return
    end

    local rewardCfg = config.Rewards

    local rewards = rewardCfg[Index]

    myData.myGoldData[Index] = 2
    
    if CheckBagGridForAwardsEx(sysarg, rewards) == 0 then
        GiveCommonAwardEx(sysarg, rewards, LogIds.OSDailyTotalRechargeLogId, Lang.LogTips.OpenServerDailyTotalRecharge)
    else
        local actorId = Actor.getActorId(sysarg)
        local title = Lang.ScriptTips.OpenServerDailyTotalRecharge001
        local content = Lang.ScriptTips.OpenServerDailyTotalRecharge002
        local nActorJob = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
        local nActorSex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
        Awards.GiveToMailById(actorId, rewards, title, content, LogIds.OSDailyTotalRechargeLogId, Lang.LogTips.OpenServerDailyTotalRecharge, nActorJob, nActorSex)
    end

    local sActorName = Actor.getName(sysarg)
    local msg = string.format(Lang.ScriptTips.OpenServerDailyTotalRecharge003, sActorName, Index, enLinkOSDailyTotalRechargeWnd)
    System.broadcastTipmsg(msg,ttScreenCenter + ttChatWindow)

    if not myData.sex then
        myData.sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
        myData.vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
    end
    

    local newData = BaseFuc_serialize(myData)
    gRankMgr_SetRankEx(actorid, RANK_OS_DAILYTOTAL_RECHARGE, 1, newData)
        
    local pack = DataPack.allocPacket(sysarg, LogicCmd.miscSys.sysId, LogicCmd.miscSys.sub.sGetOpenServerDailyTotalRechargeAward)
    if not pack then return end
    
    DataPack.writeByte(pack, Index)
    DataPack.writeByte(pack, 2)
    DataPack.flush(pack)
    
end


--累计充值元宝
function OpenServerDailyTotalRechargeWithDrawYuanbao(sysarg, Gold)
    --print("Gold ="..Gold)
    if not IsInOpenServerDay() then
        return
    end
    local day = System.getDaysSinceOpenServer()
    --print("day ="..day)
    local config = OpenServerCfg.DailyRecharge[day]
    if not config then
        return
    end 

    local actorid = Actor.getActorId(sysarg)
    local data = gRankMgr_GetValueById(actorid, RANK_OS_DAILYTOTAL_RECHARGE, 1)

    local myData = BaseFuc_unserialize(data)

    if not myData then
        myData = {}
    end
    if not myData.myGold then
        myData.myGold = 0
    end
    myData.myGold = myData.myGold + Gold
    --print("myData.myGold ="..myData.myGold)
    if not myData.myGoldData then
        myData.myGoldData = {}
    end
    for idx = 1, #config.Gold do
        if myData.myGoldData[idx] and myData.myGoldData[idx] >= 1 then
        else
            if config.Gold[idx] <= myData.myGold then
                myData.myGoldData[idx] = 1
            end
        end
    end

    if not myData.sex then
        myData.sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
        myData.vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
    end

    local newData = BaseFuc_serialize(myData)
    gRankMgr_SetRankEx(actorid, RANK_OS_DAILYTOTAL_RECHARGE, 1, newData)
    OpenServerDailyTotalRechargeSendData(sysarg)
end
